home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zdps1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.6 KB  |  485 lines

  1. /* Copyright (C) 1990, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zdps1.c,v 1.2.2.1 2000/12/02 21:08:27 raph Exp $ */
  20. /* Level 2 / Display PostScript graphics extensions */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "gsmatrix.h"
  24. #include "gspath.h"
  25. #include "gspath2.h"
  26. #include "gsstate.h"
  27. #include "ialloc.h"
  28. #include "igstate.h"
  29. #include "ivmspace.h"
  30. #include "store.h"
  31. #include "stream.h"
  32. #include "ibnum.h"
  33.  
  34. /* Forward references */
  35. private int gstate_unshare(P1(i_ctx_t *));
  36.  
  37. /* Structure descriptors */
  38. public_st_igstate_obj();
  39.  
  40. /* Extend the `copy' operator to deal with gstates. */
  41. /* This is done with a hack -- we know that gstates are the only */
  42. /* t_astruct subtype that implements copy. */
  43. private int
  44. z1copy(i_ctx_t *i_ctx_p)
  45. {
  46.     os_ptr op = osp;
  47.     int code = zcopy(i_ctx_p);
  48.  
  49.     if (code >= 0)
  50.     return code;
  51.     if (!r_has_type(op, t_astruct))
  52.     return code;
  53.     return zcopy_gstate(i_ctx_p);
  54. }
  55.  
  56. /* ------ Graphics state ------ */
  57.  
  58. /* <bool> setstrokeadjust - */
  59. private int
  60. zsetstrokeadjust(i_ctx_t *i_ctx_p)
  61. {
  62.     os_ptr op = osp;
  63.  
  64.     check_type(*op, t_boolean);
  65.     gs_setstrokeadjust(igs, op->value.boolval);
  66.     pop(1);
  67.     return 0;
  68. }
  69.  
  70. /* - currentstrokeadjust <bool> */
  71. private int
  72. zcurrentstrokeadjust(i_ctx_t *i_ctx_p)
  73. {
  74.     os_ptr op = osp;
  75.  
  76.     push(1);
  77.     make_bool(op, gs_currentstrokeadjust(igs));
  78.     return 0;
  79. }
  80.  
  81. /* ------ Graphics state objects ------ */
  82.  
  83. /*
  84.  * Check to make sure that all the elements of a graphics state
  85.  * can be stored in the given allocation space.
  86.  */
  87. /* ****** DOESN'T CHECK THE NON-REFS. ****** */
  88. private int
  89. gstate_check_space(i_ctx_t *i_ctx_p, int_gstate *isp, uint space)
  90. {
  91.     /*
  92.      * ****** WORKAROUND ALERT ******
  93.      * This code doesn't check the space of the non-refs, or copy their
  94.      * contents, so it can create dangling references from global VM to
  95.      * local VM.  Because of this, we simply disallow writing into gstates
  96.      * in global VM (including creating them in the first place) if the
  97.      * save level is greater than 0.
  98.      * ****** WORKAROUND ALERT ******
  99.      */
  100. #if 1                /* ****** WORKAROUND ****** */
  101.     if (space != avm_local && imemory_save_level(iimemory) > 0)
  102.     return_error(e_invalidaccess);
  103. #endif                /* ****** END ****** */
  104. #define gsref_check(p) store_check_space(space, p)
  105.     int_gstate_map_refs(isp, gsref_check);
  106. #undef gsref_check
  107.     return 0;
  108. }
  109.  
  110. /* - gstate <gstate> */
  111. int
  112. zgstate(i_ctx_t *i_ctx_p)
  113. {
  114.     os_ptr op = osp;
  115.  
  116.     int code = gstate_check_space(i_ctx_p, istate, icurrent_space);
  117.     igstate_obj *pigo;
  118.     gs_state *pnew;
  119.     int_gstate *isp;
  120.  
  121.     if (code < 0)
  122.     return code;
  123.     pigo = ialloc_struct(igstate_obj, &st_igstate_obj, "gstate");
  124.     if (pigo == 0)
  125.     return_error(e_VMerror);
  126.     pnew = gs_state_copy(igs, imemory);
  127.     if (pnew == 0) {
  128.     ifree_object(pigo, "gstate");
  129.     return_error(e_VMerror);
  130.     }
  131.     isp = gs_int_gstate(pnew);
  132.     int_gstate_map_refs(isp, ref_mark_new);
  133.     push(1);
  134.     /*
  135.      * Since igstate_obj isn't a ref, but only contains a ref, save won't
  136.      * clear its l_new bit automatically, and restore won't set it
  137.      * automatically; we have to make sure this ref is on the changes chain.
  138.      */
  139.     make_iastruct(op, a_all, pigo);
  140.     make_null(&pigo->gstate);
  141.     ref_save(op, &pigo->gstate, "gstate");
  142.     make_istruct_new(&pigo->gstate, 0, pnew);
  143.     return 0;
  144. }
  145.  
  146. /* copy for gstates */
  147. int
  148. zcopy_gstate(i_ctx_t *i_ctx_p)
  149. {
  150.     os_ptr op = osp;
  151.     os_ptr op1 = op - 1;
  152.     gs_state *pgs;
  153.     gs_state *pgs1;
  154.     int_gstate *pistate;
  155.     gs_memory_t *mem;
  156.     int code;
  157.  
  158.     check_stype(*op, st_igstate_obj);
  159.     check_stype(*op1, st_igstate_obj);
  160.     check_write(*op);
  161.     code = gstate_unshare(i_ctx_p);
  162.     if (code < 0)
  163.     return code;
  164.     pgs = igstate_ptr(op);
  165.     pgs1 = igstate_ptr(op1);
  166.     pistate = gs_int_gstate(pgs);
  167.     code = gstate_check_space(i_ctx_p, gs_int_gstate(pgs1), r_space(op));
  168.     if (code < 0)
  169.     return code;
  170. #define gsref_save(p) ref_save(op, p, "copygstate")
  171.     int_gstate_map_refs(pistate, gsref_save);
  172. #undef gsref_save
  173.     mem = gs_state_swap_memory(pgs, imemory);
  174.     code = gs_copygstate(pgs, pgs1);
  175.     gs_state_swap_memory(pgs, mem);
  176.     if (code < 0)
  177.     return code;
  178.     int_gstate_map_refs(pistate, ref_mark_new);
  179.     *op1 = *op;
  180.     pop(1);
  181.     return 0;
  182. }
  183.  
  184. /* <gstate> currentgstate <gstate> */
  185. int
  186. zcurrentgstate(i_ctx_t *i_ctx_p)
  187. {
  188.     os_ptr op = osp;
  189.     gs_state *pgs;
  190.     int_gstate *pistate;
  191.     int code;
  192.     gs_memory_t *mem;
  193.  
  194.     check_stype(*op, st_igstate_obj);
  195.     check_write(*op);
  196.     code = gstate_unshare(i_ctx_p);
  197.     if (code < 0)
  198.     return code;
  199.     pgs = igstate_ptr(op);
  200.     pistate = gs_int_gstate(pgs);
  201.     code = gstate_check_space(i_ctx_p, istate, r_space(op));
  202.     if (code < 0)
  203.     return code;
  204. #define gsref_save(p) ref_save(op, p, "currentgstate")
  205.     int_gstate_map_refs(pistate, gsref_save);
  206. #undef gsref_save
  207.     mem = gs_state_swap_memory(pgs, imemory);
  208.     code = gs_currentgstate(pgs, igs);
  209.     gs_state_swap_memory(pgs, mem);
  210.     if (code < 0)
  211.     return code;
  212.     int_gstate_map_refs(pistate, ref_mark_new);
  213.     return 0;
  214. }
  215.  
  216. /* <gstate> setgstate - */
  217. int
  218. zsetgstate(i_ctx_t *i_ctx_p)
  219. {
  220.     os_ptr op = osp;
  221.     int code;
  222.  
  223.     check_stype(*op, st_igstate_obj);
  224.     check_read(*op);
  225.     code = gs_setgstate(igs, igstate_ptr(op));
  226.     if (code < 0)
  227.     return code;
  228.     pop(1);
  229.     return 0;
  230. }
  231.  
  232. /* ------ Rectangles ------- */
  233.  
  234. /*
  235.  * We preallocate a short list for rectangles, because
  236.  * the rectangle operators usually will involve very few rectangles.
  237.  */
  238. #define MAX_LOCAL_RECTS 5
  239. typedef struct local_rects_s {
  240.     gs_rect *pr;
  241.     uint count;
  242.     gs_rect rl[MAX_LOCAL_RECTS];
  243. } local_rects_t;
  244.  
  245. /* Forward references */
  246. private int rect_get(P3(local_rects_t *, os_ptr, gs_memory_t *));
  247. private void rect_release(P2(local_rects_t *, gs_memory_t *));
  248.  
  249. /* <x> <y> <width> <height> .rectappend - */
  250. /* <numarray|numstring> .rectappend - */
  251. private int
  252. zrectappend(i_ctx_t *i_ctx_p)
  253. {
  254.     os_ptr op = osp;
  255.     local_rects_t lr;
  256.     int npop = rect_get(&lr, op, imemory);
  257.     int code;
  258.  
  259.     if (npop < 0)
  260.     return npop;
  261.     code = gs_rectappend(igs, lr.pr, lr.count);
  262.     rect_release(&lr, imemory);
  263.     if (code < 0)
  264.     return code;
  265.     pop(npop);
  266.     return 0;
  267. }
  268.  
  269. /* <x> <y> <width> <height> rectclip - */
  270. /* <numarray|numstring> rectclip - */
  271. private int
  272. zrectclip(i_ctx_t *i_ctx_p)
  273. {
  274.     os_ptr op = osp;
  275.     local_rects_t lr;
  276.     int npop = rect_get(&lr, op, imemory);
  277.     int code;
  278.  
  279.     if (npop < 0)
  280.     return npop;
  281.     code = gs_rectclip(igs, lr.pr, lr.count);
  282.     rect_release(&lr, imemory);
  283.     if (code < 0)
  284.     return code;
  285.     pop(npop);
  286.     return 0;
  287. }
  288.  
  289. /* <x> <y> <width> <height> rectfill - */
  290. /* <numarray|numstring> rectfill - */
  291. private int
  292. zrectfill(i_ctx_t *i_ctx_p)
  293. {
  294.     os_ptr op = osp;
  295.     local_rects_t lr;
  296.     int npop = rect_get(&lr, op, imemory);
  297.     int code;
  298.  
  299.     if (npop < 0)
  300.     return npop;
  301.     code = gs_rectfill(igs, lr.pr, lr.count);
  302.     rect_release(&lr, imemory);
  303.     if (code < 0)
  304.     return code;
  305.     pop(npop);
  306.     return 0;
  307. }
  308.  
  309. /* <x> <y> <width> <height> rectstroke - */
  310. /* <numarray|numstring> rectstroke - */
  311. private int
  312. zrectstroke(i_ctx_t *i_ctx_p)
  313. {
  314.     os_ptr op = osp;
  315.     gs_matrix mat;
  316.     local_rects_t lr;
  317.     int npop, code;
  318.  
  319.     if (read_matrix(op, &mat) >= 0) {
  320.     /* Concatenate the matrix to the CTM just before stroking the path. */
  321.     npop = rect_get(&lr, op - 1, imemory);
  322.     if (npop < 0)
  323.         return npop;
  324.     code = gs_rectstroke(igs, lr.pr, lr.count, &mat);
  325.     npop++;
  326.     } else {
  327.     /* No matrix. */
  328.     npop = rect_get(&lr, op, imemory);
  329.     if (npop < 0)
  330.         return npop;
  331.     code = gs_rectstroke(igs, lr.pr, lr.count, (gs_matrix *) 0);
  332.     }
  333.     rect_release(&lr, imemory);
  334.     if (code < 0)
  335.     return code;
  336.     pop(npop);
  337.     return 0;
  338. }
  339.  
  340. /* --- Internal routines --- */
  341.  
  342. /* Get rectangles from the stack. */
  343. /* Return the number of elements to pop (>0) if OK, <0 if error. */
  344. private int
  345. rect_get(local_rects_t * plr, os_ptr op, gs_memory_t *mem)
  346. {
  347.     int format, code;
  348.     uint n, count;
  349.     gs_rect *pr;
  350.     double rv[4];
  351.  
  352.     switch (r_type(op)) {
  353.     case t_array:
  354.     case t_mixedarray:
  355.     case t_shortarray:
  356.     case t_string:
  357.         code = num_array_format(op);
  358.         if (code < 0)
  359.         return code;
  360.         format = code;
  361.         count = num_array_size(op, format);
  362.         if (count % 4)
  363.         return_error(e_rangecheck);
  364.         count /= 4;
  365.         break;
  366.     default:        /* better be 4 numbers */
  367.         code = num_params(op, 4, rv);
  368.         if (code < 0)
  369.         return code;
  370.         plr->pr = plr->rl;
  371.         plr->count = 1;
  372.         plr->rl[0].q.x = (plr->rl[0].p.x = rv[0]) + rv[2];
  373.         plr->rl[0].q.y = (plr->rl[0].p.y = rv[1]) + rv[3];
  374.         return 4;
  375.     }
  376.     plr->count = count;
  377.     if (count <= MAX_LOCAL_RECTS)
  378.     pr = plr->rl;
  379.     else {
  380.     pr = (gs_rect *)gs_alloc_byte_array(mem, count, sizeof(gs_rect),
  381.                         "rect_get");
  382.     if (pr == 0)
  383.         return_error(e_VMerror);
  384.     }
  385.     plr->pr = pr;
  386.     for (n = 0; n < count; n++, pr++) {
  387.     ref rnum;
  388.     int i;
  389.  
  390.     for (i = 0; i < 4; i++) {
  391.         code = num_array_get((const ref *)op, format,
  392.                  (n << 2) + i, &rnum);
  393.         switch (code) {
  394.         case t_integer:
  395.             rv[i] = rnum.value.intval;
  396.             break;
  397.         case t_real:
  398.             rv[i] = rnum.value.realval;
  399.             break;
  400.         default:    /* code < 0 */
  401.             return code;
  402.         }
  403.     }
  404.     pr->q.x = (pr->p.x = rv[0]) + rv[2];
  405.     pr->q.y = (pr->p.y = rv[1]) + rv[3];
  406.     }
  407.     return 1;
  408. }
  409.  
  410. /* Release the rectangle list if needed. */
  411. private void
  412. rect_release(local_rects_t * plr, gs_memory_t *mem)
  413. {
  414.     if (plr->pr != plr->rl)
  415.     gs_free_object(mem, plr->pr, "rect_release");
  416. }
  417.  
  418. /* ------ Graphics state ------ */
  419.  
  420. /* <llx> <lly> <urx> <ury> setbbox - */
  421. int
  422. zsetbbox(i_ctx_t *i_ctx_p)
  423. {
  424.     os_ptr op = osp;
  425.     double box[4];
  426.  
  427.     int code = num_params(op, 4, box);
  428.  
  429.     if (code < 0)
  430.     return code;
  431.     if ((code = gs_setbbox(igs, box[0], box[1], box[2], box[3])) < 0)
  432.     return code;
  433.     pop(4);
  434.     return 0;
  435. }
  436.  
  437. /* ------ Initialization procedure ------ */
  438.  
  439. const op_def zdps1_l2_op_defs[] =
  440. {
  441.     op_def_begin_level2(),
  442.         /* Graphics state */
  443.     {"0currentstrokeadjust", zcurrentstrokeadjust},
  444.     {"1setstrokeadjust", zsetstrokeadjust},
  445.         /* Graphics state objects */
  446.     {"1copy", z1copy},
  447.     {"1currentgstate", zcurrentgstate},
  448.     {"0gstate", zgstate},
  449.     {"1setgstate", zsetgstate},
  450.         /* Rectangles */
  451.     {"1.rectappend", zrectappend},
  452.     {"1rectclip", zrectclip},
  453.     {"1rectfill", zrectfill},
  454.     {"1rectstroke", zrectstroke},
  455.         /* Graphics state components */
  456.     {"4setbbox", zsetbbox},
  457.     op_def_end(0)
  458. };
  459.  
  460. /* ------ Internal routines ------ */
  461.  
  462. /* Ensure that a gstate is not shared with an outer save level. */
  463. /* *op is of type t_astruct(igstate_obj). */
  464. private int
  465. gstate_unshare(i_ctx_t *i_ctx_p)
  466. {
  467.     os_ptr op = osp;
  468.     ref *pgsref = &r_ptr(op, igstate_obj)->gstate;
  469.     gs_state *pgs = r_ptr(pgsref, gs_state);
  470.     gs_state *pnew;
  471.     int_gstate *isp;
  472.  
  473.     if (!ref_must_save(pgsref))
  474.     return 0;
  475.     /* Copy the gstate. */
  476.     pnew = gs_gstate(pgs);
  477.     if (pnew == 0)
  478.     return_error(e_VMerror);
  479.     isp = gs_int_gstate(pnew);
  480.     int_gstate_map_refs(isp, ref_mark_new);
  481.     ref_do_save(op, pgsref, "gstate_unshare");
  482.     make_istruct_new(pgsref, 0, pnew);
  483.     return 0;
  484. }
  485.